﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Transactions;

namespace DAL {
    class SubjectRepository {
        //Get Primary Key for the Current Table
        public static string Getpk(string brnchid, string orgid) {
            SMSEntities Sms = new SMSEntities();
            var generated = Sms.GetPrimaryKey("Subject").ToList();
            return String.Format("SBA{0}.{1}-{2:00000000000000}", orgid, brnchid, Convert.ToInt32(generated[0].number));
        }
        //Start the Session 
        public static bool CreateSubject(string SubjectName, string SessionID, string OrgID, string BranchID) {

            using (TransactionScope transaction = new TransactionScope()) {
                bool IsCreated = false;
                SMSEntities Sms = new SMSEntities();
                Subject ISubject = new Subject();
                ISubject.Active = true;
                ISubject.BranchID = BranchID;
                ISubject.CDate = System.DateTime.Now;
                ISubject.OrgID = OrgID;
                ISubject.SessionID = SessionID;
                ISubject.SubjectID = Getpk(BranchID, OrgID);
                ISubject.SubjectName = SubjectName;
                Sms.Subjects.Add(ISubject);
                Sms.SaveChanges();
                transaction.Complete();
                IsCreated = true;
                return IsCreated;
            }
        }
        public static bool CreateSubject(string SubjectID, string SubjectName, string SessionID, string OrgID, string BranchID) {

            using (TransactionScope transaction = new TransactionScope()) {
                bool IsCreated = false;
                SMSEntities Sms = new SMSEntities();
                Subject ISubject = new Subject();
                ISubject.Active = true;
                ISubject.BranchID = BranchID;
                ISubject.CDate = System.DateTime.Now;
                ISubject.OrgID = OrgID;
                ISubject.SessionID = SessionID;
                ISubject.SubjectID = SubjectID;
                ISubject.SubjectName = SubjectName;
                Sms.Subjects.Add(ISubject);
                Sms.SaveChanges();
                transaction.Complete();
                IsCreated = true;
                return IsCreated;
            }
        }
        public static List<GetAllSubject_Result> GetAllSubjectActive(string OrgID, string BranchID) {

            SMSEntities Sms = new SMSEntities();
            return Sms.GetAllSubject(OrgID, BranchID).ToList<GetAllSubject_Result>();
        }
    }
}
